package com.fmob.client.app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.fmob.client.app.R;
import com.tencent.bugly.crashreport.CrashReport;

import cn.jpush.android.api.JPushInterface;
import sdk.webview.fmc.com.fmcsdk.activity.LoginActivity;
import sdk.webview.fmc.com.fmcsdk.app.AppManager;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.bean.User;
import sdk.webview.fmc.com.fmcsdk.check.activity.CheckDetailsActivity;
import sdk.webview.fmc.com.fmcsdk.interfaces.FristView;
import sdk.webview.fmc.com.fmcsdk.presenter.FristPresenter;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;
import sdk.webview.fmc.com.fmcsdk.util.RootUtils;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.view.FirstXiyiDialog;

/**
 * APP启动的第一个Activity
 */
public class FirstActivity extends BaseActivity<FristView, FristPresenter> implements FristView {

    SharedPreferences.Editor editor;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_frist;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        String appName = (String) Utility.getMetaData(this, Constant.APP_NAME);
        if (appName.equals(Constant.APP_NAME_EAM)) {
            if (RootUtils.hasRootPrivilege()) {
                ToastUtils.showShort(this, "手机被Root,不允许访问");
                AppManager.getAppManager().appExit();
                return;
            }
        }

        editor = sharedPrefs.edit();

        String firstOpen = sharedPrefs.getString(Constant.FIRST_OPEN, "");
        boolean firstOpenAgress = sharedPrefs.getBoolean(Constant.FIRST_OPEN_AGREE, false);

//        String xieyiStr = StringUtils.getActivityPlaceholders(this, "YINSI_XIEYI_URL");

        //判断是否还有用户信息，如果没有的话跳转到登录界面
        PreferencesHelper.init(context);
        User data = PreferencesHelper.getData(User.class);
        if (data == null) {
            XLog.i("data为null");
            if (firstOpenAgress) {
                XLog.i("firstOpenAgress--->" + firstOpenAgress);
                toLogin();
            } else {
                final FirstXiyiDialog dialog = new FirstXiyiDialog(this, new FirstXiyiDialog.ClickiInterface() {
                    @Override
                    public void clickSure() {
                        editor.putBoolean(Constant.FIRST_OPEN_AGREE, true).apply();
                        // 初始化需要授权的sdk
                        try {
                            AppAutoInitializer.getInstance().initNeedPermissionsSDK(context);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        toLogin();
                    }

                    @Override
                    public void clickCancel() {
                        System.exit(0);
                    }
                });
                dialog.show();
            }

        } else {
            //如果是第一次登陆APP,没有欢迎页直接跳转到登录界面
            if (TextUtils.isEmpty(firstOpen)) {
                if (firstOpenAgress) {
                    toLogin();
                } else {

                    final FirstXiyiDialog dialog = new FirstXiyiDialog(this, new FirstXiyiDialog.ClickiInterface() {
                        @Override
                        public void clickSure() {
                            // 初始化需要授权的sdk
                            try {
                                AppAutoInitializer.getInstance().initNeedPermissionsSDK(context);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            editor.putBoolean(Constant.FIRST_OPEN_AGREE, true).apply();
                            toLogin();
                        }

                        @Override
                        public void clickCancel() {
                            System.exit(0);
                        }
                    });
                    dialog.show();
                }
            } else {
                initlocation();
                startActivity(new Intent(this, WelcomeActivity.class));
                finish();
            }
        }

        // 设置默认声音和振动提示
        if (!sharedPrefs.contains(Constant.SOUND)) {
            editor.putBoolean(Constant.SOUND, true).apply();
        }
        if (!sharedPrefs.contains(Constant.VIBRATE)) {
            editor.putBoolean(Constant.VIBRATE, true).apply();
        }
        //设置默认非WIFI也上传图片
        if (!sharedPrefs.contains(Constant.WIFI_UP)) {
            editor.putBoolean(Constant.WIFI_UP, false).apply();
        }
        //设置是否开启离线
        if (!sharedPrefs.contains(Constant.OPEN_OFFLINE_MODE)) {
            editor.putBoolean(Constant.OPEN_OFFLINE_MODE, false).apply();
        }

    }

    private void toLogin() {
        initlocation();
        startActivity(new Intent(FirstActivity.this, LoginActivity.class));
        finish();
    }

    private void initlocation() {
        SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
        SDKInitializer.initialize(getApplicationContext());

        CrashReport.initCrashReport(getApplicationContext(), "c729714f2f", true);
    }

    @Override
    protected FristPresenter createPresenter() {
        return new FristPresenter();
    }
}
